﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Swin_e_mon_Forms
{
    public class TransferCommand:Command
    {
        public TransferCommand(SwinEForm f)
            : base(new string[] {"drop", "pickup", "take", "put"}, f)
        { }

        public override string Run(Player p, string[] input)
        {

            if (input.Length == 2 && input[0].ToLower() == "drop")
                return DoDrop(p, input[1]);
            else if (input.Length == 2 && (input[0].ToLower() == "pickup" || input[0].ToLower() == "take"))
                return PickUp(p, input[1]);
            else if (input.Length == 4 && (input[0].ToLower() == "put" || input[2].ToLower() == "in"))
                return PutIn(p, input[1], input[3]);
            else if (input.Count() == 4 && (input[0].ToLower() == "take" || input[2].ToLower() == "from"))
                return TakeFrom(p, input[1], input[3]);
            else if (input.Count() == 1)
                return input[0] + " what?";

            return "I do not know how to transfer an item like that";
        }

        public string DoDrop(Player p, string id)
        {
            Item item = p.Inventory.AccessItem(id);
            if (item.IsStationary) return "You cannot move a stationary item";
            item = p.Inventory.TakeItem(id);
            if (item == null)
                return "You do not have " + id;


            f.RemoveFromPlayerList(item.Title + " (" + item.mainIdentifier() + ") ");
            p.InLocation.Inventory.AddItem(item);
            f.AddToLocList(item.Title + " (" + item.mainIdentifier() + ") ");

            return "You have dropped " + id;
        }

        public string PickUp(Player p, string id)
        {
            Item item = p.InLocation.Inventory.AccessItem(id);
            if (!item.IsStationary) p.InLocation.Inventory.TakeItem(id);

            if (item == null) 
                return "I don't see any " + id + " around.";
            if (item.IsStationary)
                return "You cannot pick up a " + id;

            f.AddToPlayerList(item.Title + " (" + item.mainIdentifier() + ") ");
            p.Inventory.AddItem(item);
            f.RemoveFromLocList(item.Title + " (" + item.mainIdentifier() + ") ");

            return "You have picked up " + id;
        }

        public string PutIn(Player p, string id, string container)
        {
            if (id == container)
                return "You cant put a " + id + " in a " + container;

            Item item = p.Inventory.TakeItem(id);
            GameEntity aContainer = p.LocateEntity(container);

            if (item == null)
                return "I don't see any " + id + " around.";
            else if (aContainer == null)
                return "I don't see " + container;
            else if (!aContainer.HasInventory)
                return "You cannot put items in " + aContainer.Title;

            f.RemoveFromPlayerList(item.Title + " (" + item.mainIdentifier() + ") ");
            aContainer.Inventory.AddItem(item);

            return "The " + id + " has been placed inside " + container;
        }

        public string TakeFrom(Player p, string id, string container)
        {
            GameEntity aContainer = p.LocateEntity(container);
            Item item = aContainer.Inventory.TakeItem(id);

            if (item == null)
                return "I don't see any " + id + " around.";
            else if (aContainer == null)
                return "I don't see " + container;
            else if (!aContainer.HasInventory)
                return aContainer + " does not have an inventory!";

            f.AddToPlayerList(item.Title + " (" + item.mainIdentifier() + ") ");
            p.Inventory.AddItem(item);

            return "The " + id + " has been placed inside your inventory";
        }
    }
}
